<template>
    <div class="course-detail-page">
        <el-collapse v-model="activeNames">
            <el-collapse-item
                v-for="item in list"
                :key="item.id"
                :title="`${item.name} ${item.count}个`"
                :name="item.id">
                <div
                    :class="`item flex ${val.active ? 'active' : ''}`"
                    v-for="(val, i) in item.contents"
                    :key="i"
                    @click="onSelected(val)">
                    <div class="item-num">{{ i + 1 > 9 ? i + 1 : `0${i + 1}` }}</div>
                    <div class="item-content">
                        <p class="item-title">{{ val.name }}</p>
                        <p class="item-desc">
                            {{ val.description }}
                        </p>
                    </div>
                </div>
            </el-collapse-item>
        </el-collapse>
        <div class="text-center">
            <el-button @click="onJump" type="text" class="btn">生成你的团队学习建议提案</el-button>
        </div>
        <p class="tip">
            我们会根据你的选择，为你生成来自专家的学习建议在线清单，你还可以下载到本地作为提案文档。
        </p>
    </div>
</template>

<script>
import {
    Collapse,
    CollapseItem,
} from 'element-ui';

export default {
    components: {
        ElCollapse: Collapse,
        ElCollapseItem: CollapseItem,
    },
    data() {
        return {
            id: '',
            activeNames: [],
            list: [],

            selectId: [],
        };
    },
    methods: {
        onSave() {

        },

        fetchData() {
            API.get(`../elective/${this.id}`).then(res => {
                const list = res.abilitys || [];

                list.map(item => {
                    item = item.contents.map(items => {
                        items.active = false;
                        return items;
                    });
                    return item;
                });

                if (list.length) this.activeNames.push(list[0].id);

                this.list = list;
            });
        },

        onSelected(e) {
            e.active = !e.active;

            const selectId = [...this.selectId];
            if (selectId.indexOf(e.id) > -1) {
                selectId.splice(selectId.indexOf(e.id), 1);
            } else {
                selectId.push(e.id);
            }

            this.selectId = selectId;
        },

        onJump() {
            const id = this.id;
            const theme = this.$route.query.theme;
            const selectId = this.selectId;

            if (!selectId.length) return $ele.$message.warning('请选择内容');

            let idStr = '';

            for (const val of selectId) {
                idStr = `${idStr}${val},`;
            }
            idStr = idStr.slice(0, -1);

            this.$router.push({
                name: 'Course.Selection.Result',
                query: {
                    id,
                    theme,
                    idStr,
                },
            });
        },
    },
    mounted() {
        const id = this.$route.query.id;

        if (id) {
            this.id = id;
            this.fetchData();
        }
    },
};
</script>

<style lang="scss">
.course-detail-page {
    .el-collapse {
        margin: 30px 0 65px 0;
        border: none;

        @media (max-width: 960px) {
            margin: 0;
        }

        .el-collapse-item__header {
            font-size: 24px;
            color: rgb(74, 74, 74);
            height: auto;
            line-height: inherit;
            padding: 15px 0;
            border-bottom: none;

            @media (max-width: 960px) {
                font-size: 18px;
                font-weight: bold;
                line-height: 25px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 0;
                margin-bottom: 18px;
            }
        }

        .el-collapse-item__wrap {
            border-bottom: none;
        }

        .el-collapse-item__arrow::before {
            content: '';
        }

        .el-collapse-item__arrow {
            width: 30px;
            height: 30px;
            background: url('~@/assets/images/course/open.png') no-repeat;
            background-size: contain;

            @media (max-width: 960px) {
                margin: 0;
            }
        }

        .el-collapse-item__arrow.is-active {
            background: url('~@/assets/images/course/close.png') no-repeat;
            transform: rotate(0);
        }

        .el-collapse-item__content {
            @media (max-width: 960px) {
                padding-bottom: 0;
                margin-bottom: 16px;
            }
        }

        .item {
            width: 100%;
            padding: 17px 26px 20px 17px;
            box-sizing: border-box;
            background: rgb(249, 249, 249);
            border-radius: 5px;
            margin-bottom: 12px;

            @media (max-width: 960px) {
                padding: 14px 17px 20px 14px;
                margin-bottom: 10px;
            }

            .item-num {
                height: fit-content;
                font-size: 28px;
                line-height: 28px;
                color: rgb(215, 215, 215);
                margin-right: 17px;
                font-weight: bold;

                @media (max-width: 960px) {
                    font-size: 20px;
                    line-height: 22px;
                    margin-right: 9px;
                }
            }

            .item-title {
                font-size: 20px;
                font-weight: bold;
                margin-bottom: 10px;
                line-height: 28px;
                color: rgb(74, 74, 74);

                @media (max-width: 960px) {
                    font-size: 16px;
                    line-height: 22px;
                    margin-bottom: 5px;
                }
            }

            .item-desc {
                font-size: 16px;
                color: rgb(74, 74, 74);

                @media (max-width: 960px) {
                    font-size: 12px;
                    line-height: 17px;
                }
            }
        }

        .item:last-child {
            margin-bottom: 0;
        }

        .item:hover {
            @media (min-width: 961px) {
                cursor: pointer;
                .item-num, .item-title, .item-desc {
                    color: rgb(45, 112, 174);
                }
            }
        }

        .item.active {
            .item-num, .item-title, .item-desc {
                color: white;
            }

            background: rgb(45, 112, 174);
        }
    }

    .btn {
        width: 425px;
        height: 64px;
        background: rgb(45, 112, 174);
        color: white;
        font-size: 24px;
        border-radius: 8px;

        @media (max-width: 960px) {
            width: 259px;
            height: 42px;
            font-size: 16px;
            border-radius: 5px;
            margin-top: 45px;
        }
    }

    .btn:hover {
        box-shadow: 0 0 15px 0 rgb(117, 117, 117);
    }

    .btn:active {
        box-shadow: 0 0 10px 0 rgb(117, 117, 117);
    }

    .tip {
        margin-top: 25px;
        text-align: center;
        color: rgb(180, 180, 180);

        @media (max-width: 960px) {
            font-size: 12px;
            line-height: 17px;
            margin-top: 13px;
        }
    }
}
</style>
